/*
 * Copyright 2012 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package be.free.ts.timesheet;

import be.free.ts.activity.Activity;
import be.free.ts.employee.Employee;
import be.free.ts.support.AbstractPersistable;
import be.free.ts.support.Interval;
import be.free.ts.support.validation.ValidInterval;
import org.eclipse.persistence.annotations.Convert;
import org.joda.time.DateTime;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.Date;

/**
 * @author Christophe De Blende
 * @since 08/12/12
 */
@Entity
public class Timesheet extends AbstractPersistable {

	protected Timesheet() {
		// use the builder
	}

	@NotNull
	@Temporal(TemporalType.TIMESTAMP)
	@Convert("DateTimeConverter")
	private DateTime submittedDateTime;

	@Lob
	@Size(min = 3)
	private String description;

	@ManyToOne
	private Activity activity;

	@NotNull
	@ManyToOne
	private Employee employee;

	@NotNull
	@Embedded
	@ValidInterval
	private Interval interval;

	public Interval getInterval() {
		return interval;
	}

	public DateTime getSubmittedDateTime() {
		return submittedDateTime;
	}

	public Date getSubmittedDate() {
		if (submittedDateTime == null) {
			return null;
		}
		return submittedDateTime.toDate();
	}

	public String getDescription() {
		return description;
	}

	public Activity getActivity() {
		return activity;
	}

	public Employee getEmployee() {
		return employee;
	}

	public void setSubmittedDateTime(DateTime submittedDateTime) {
		this.submittedDateTime = submittedDateTime;
	}

	public void setSubmittedDate(Date submittedDate) {
		this.submittedDateTime = submittedDate == null ? null : new DateTime(submittedDate);
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public void setActivity(Activity activity) {
		this.activity = activity;
	}

	public void setEmployee(Employee employee) {
		this.employee = employee;
	}

	public void setInterval(Interval interval) {
		this.interval = interval;
	}

}
